<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="60%" destroy-on-close @closed="$emit('closed')" align-center>
        <el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="100px" label-position="right">
            <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="承制人">
                        <el-input v-model="info.referred_as" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="发缝合同号" prop="contract_no">
                        <el-input v-model="form.contract_no" v-if="contractShow" readonly></el-input>
                        <el-select v-model="form.contract_no" clearable style="width: 100%" placeholder="请选择" v-else @change="conChange" filterable>
                            <el-option v-for="item in contractList" :key="item.contract_no" :label="item.contract_title" :value="item.contract_no" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="工厂货号">
                        <el-input v-model="info.factory_number" readonly></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="批次号">
                       <el-input v-model="info.batch_number" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="工序" prop="two_process_id">
                        <el-select v-model="form.two_process_id" clearable style="width: 100%" placeholder="请选择" @change="processChange" multiple>
                            <el-option v-for="item in twoProcess" :key="item.id" :label="item.title" :value="item.id" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="收货日期" prop="take_deliver_date">
                        <el-date-picker v-model="form.take_deliver_date" :value-format="'YYYY-MM-DD'" type="data" placeholder="请选择收货日期" style="width: 100%" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="备注" prop="remark">
                        <el-input v-model="form.remark" placeholder="请输入备注"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-container>
                <el-col :span="24">
                    <div style="text-align: center;margin: 10px;">发缝资料</div>
                    <sc-form-table ref="table" v-model="form.size_data" :addTemplate="material" placeholder="暂无数据" hideAdd hideDelete height="400">
                        <el-table-column label="颜色" prop="colour" align="center"></el-table-column>
                        <el-table-column label="批次号" prop="batch_number" align="center"></el-table-column>
                        <el-table-column label="尺码" prop="goods_size" align="center"></el-table-column>
                        <el-table-column label="可收数" prop="can_take_num" align="center"></el-table-column>
                        <el-table-column label="此次收货数量" prop="num" align="center">
                            <template #default="scope">
                                <el-input v-model="scope.row.num" placeholder="请输入此次收货数量" @input="input(scope.row)"></el-input>
                            </template>
                        </el-table-column>
                    </sc-form-table>
                </el-col>
            </el-container>
        </el-form>
        <el-footer>
            <div style="display:flex;justify-content: end;">
                <el-button @click="visible = false">取 消</el-button>
                <el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit(1)">保 存</el-button>
                <el-button :loading="isSaveing" type="primary" @click="submit(2)">保存并打印</el-button>
            </div>
        </el-footer>
	</el-dialog>
</template>

<script>
    export default {
        emits: ['success', 'closed'],
        data() {
            return {
                mode: "add",
                titleMap: {
                    add: '缝挑收货录入',
                    edit: '编辑仓库',
                    show: '查看'
                },
                visible: false,
                isSaveing: false,
                //表单数据
                form: {
                    remark:"",
                    take_deliver_date: new Date().toISOString().slice(0, 10),
                    size_data:[],
                    process_id: 4,
                    contract_no:"",
                    two_process_id:""
                },
                info:{
                    qrcode:"",
                    referred_as: "",
                    contract_no: "",
                    factory_number: "",
                    batch_number: "",
                    produce_process_name: "",
                    weight:"",
                },
                rules: {
                    contract_no: [
                        { required: true, message: '请选择合同' }
                    ],
					two_process_id: [
						{required: true, message: '请选择工序'}
					],
                },
                twoProcess:[],
                contractShow:false,
                contractList:[],
            }
        },
        methods: {
            //显示
            open(mode = 'add') {
                this.mode = mode;
                this.visible = true;
                return this
            },
            //表单提交方法
            submit(e) {
                this.$refs.dialogForm.validate(async (valid) => {
                    if (valid) {
                        this.isSaveing = true;
                        var res = [];
                        res = await this.$API.produce.sewInput.post(this.form);
                        this.isSaveing = false;
                        if (res.code == 1) {
                            this.visible = false;
                            if(e==1){
                                this.$emit('success', this.form, this.mode)
                                this.$message.success("操作成功")
                            }else{
                                var reqData = { deliver_no: res.data.deliver_no, contract_no:res.data.contract_no};
                                var print = await this.$API.print.sendPrint.get(reqData);
                                if (print.code == 1) {
                                    this.$emit('success', this.form, this.mode,print.data)
                                } else {
                                    this.$alert(print.message, "提示", { type: "error" });
                                }
                            }
                        } else {
                            this.$alert(res.message, "提示", { type: 'error' })
                        }
                    } else {
                        return false;
                    }
                })
            },
            //表单注入数据
            async setData(data) {
                let array = []
                if(!data){
                    this.contractShow = false
                    var res = await this.$API.produce.proAll.get({process_id:4});
                    // this.contractList = res.data
                    res.data.map(item=>{
                        this.contractList.push({
                            contract_no:item.contract_no,
                            contract_title : item.contract_no + ' - ' + item.referred_as + ' - ' + item.factory_number + ' - ' + item.batch_number
                        })
                    })
                }else{
                    this.contractShow = true
                    data.orderDetail.map(item=>{
                        array.push({
                            colour:item.colour,
                            batch_number:data.batch_number,
                            goods_size:item.goods_size,
                            can_take_num:item.can_take_num,
                            num:item.can_take_num,
                            dozen_weight:item.dozen_weight,
                            weight:item.weight,
                            contract_no:item.contract_no,
                            total_weight:item.total_weight,
                            goods_id:item.goods_id,
                        })
                    })
                    this.form.size_data = array
                    this.info.referred_as = data.apply_name
                    this.form.contract_no = data.contract_no
                    this.info.factory_number = data.factory_number
                    this.info.batch_number = data.batch_number
	                this.twoProcess = data.sub_process;
                    let proArray = []
                    let proName = []
                    if(data.sub_process!=''){
                        data.sub_process.map(item=>{
                            proArray.push(
                                item.id
                            )
                            proName.push(
                                item.title
                            )
                        })
                        this.form.two_process_id = proArray
                        this.info.produce_process_name = proName.join("-")
                    }
                }
            },
            async conChange(e){
                var res = await this.$API.produce.proDetail.get({contract_no:e});
                let array = []
                this.twoProcess = res.data.sub_process;
                let proArray = []
                if(res.data.sub_process!=''){
                    res.data.sub_process.map(item=>{
                            proArray.push(
                                item.id
                            )
                        })
                    this.form.two_process_id = proArray
                }else{
                    this.form.two_process_id = ''
                }
                this.info.referred_as = res.data.apply_name
	            this.info.factory_number = res.data.factory_number
	            this.info.batch_number = res.data.batch_number
                res.data.orderDetail.map(item=>{
                    array.push({
                        colour:item.colour,
                        batch_number:res.data.batch_number,
                        goods_size:item.goods_size,
                        can_take_num:item.can_take_num,
                        num:item.can_take_num,
                        dozen_weight:item.dozen_weight,
                        weight:item.weight,
                        contract_no:item.contract_no,
                        total_weight:item.total_weight,
                        goods_id:item.goods_id
                    })
                })
                this.form.size_data = array
                this.twoProcess = res.data.sub_process;
            },
            processChange(e){
                console.log('e :>> ', e);
                // this.twoProcess.map(item=>{
                //     if(e==item.id){
                //         this.info.produce_process_name = item.title
                //     }
                // })
            },
            input(row){
                if(row.num > row.can_take_num){
                    this.$message.error("超过最大可收数")
                    row.num = row.can_take_num
                }
            },
        }
    }
</script>

<style></style>
